package com.ll.spring5.testdemo;

import com.ll.spring5.autowire.Emp;
import com.ll.spring5.bean.Orders;
import com.ll.spring5.bean.Student;
import com.ll.spring5.collectiontype.Book;
import com.ll.spring5.collectiontype.Course;
import com.ll.spring5.collectiontype.Stu;
import com.ll.spring5.factorybean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 2020/9/7 - 20:52
 */
public class TestSpring5Demo1 {
    @Test
    public void test() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        Stu stu = context.getBean("stu", Stu.class);
        stu.test();
    }

    @Test
    public void test2() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
        //默认是单实例对象,获取对象的地址是一样的
        Book book = context.getBean("book", Book.class);
        Book book2 = context.getBean("book", Book.class);
        System.out.println(book);
        System.out.println(book2);
    }

    @Test
    public void test3() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
        Course course = context.getBean("myBean", Course.class);
        System.out.println(course);
    }

    //生命周期
    @Test
    public void test4() {
        ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("bean4.xml");
        Orders orders = classPathXmlApplicationContext.getBean("orders", Orders.class);
        System.out.println(orders);

        //手动让bean实例销毁
        ((ClassPathXmlApplicationContext)classPathXmlApplicationContext).close();

    }

    //自动注入
    @Test
    public void test5() {
        ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("bean5.xml");
        Emp emp = classPathXmlApplicationContext.getBean("emp", Emp.class);
        emp.test();
    }

    //bean的生命周期
    @Test
    public void test6() {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("bean4.xml");
        Student stu = classPathXmlApplicationContext.getBean("stu", Student.class);
        System.out.println(stu);

    }


}

